import {
  createTenant,
  updateTenant,
  deleteTenant,
  findTenants,
  findTenant,

} from '../services/tenant';

export default {
  namespace: 'tenant',
  state: {},

  effects: {

    * createTenant({payload}, {call, put}) {
      const response = yield call(createTenant, payload);
      if (response) {
        yield put({
          type: 'onDataLoad',
          payload: {
            tenant: response.result,
          },
        });
      }
      return response;
    },
    * updateTenant({payload}, {call, put}) {
      const response = yield call(updateTenant, payload);
      if (response) {
        yield put({
          type: 'onDataLoad',
          payload: {
            tenant: response.result,
          },
        });
      }
      return response;
    },
    * deleteTenant({payload}, {call, put}) {
      console.log('delete tenant', payload);
      const response = yield call(deleteTenant, payload);
      return response;
    },

    * findTenant({payload}, {call, put}) {
      const response = yield call(findTenant, payload);
      if (response) {
        yield put({
          type: 'onDataLoad',
          payload: {
            tenants: response.result,
          },
        });
      }
      return response;
    },

    * findTenants({payload}, {call, put}) {
      const response = yield call(findTenants, payload);
      if (response) {
        yield put({
          type: 'onDataLoad',
          payload: {
            tenants: response.result,
          },
        });
      }
      return response;
    },
  },

  reducers: {
    onDataLoad(state, {payload}) {
      return {...state, ...payload};
    },
  },

};
